package com.palm.hubo2o.ui;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.aurelhubert.ahbottomnavigation.AHBottomNavigation;
import com.aurelhubert.ahbottomnavigation.AHBottomNavigationItem;
import com.palm.hubo2o.BaseActivity;
import com.palm.hubo2o.R;
import com.palm.hubo2o.network.PalmSession;
import com.palm.hubo2o.ui.fragment.CenterFragment;
import com.palm.hubo2o.ui.fragment.HallFragment;
import com.palm.hubo2o.ui.fragment.OrderListFragment;
import com.palm.palmutils.DrawableUtils;

import butterknife.Bind;

/**
 * Created by dest1 on 2015/10/20.
 */
public class MainActivity extends BaseActivity implements AHBottomNavigation.OnTabSelectedListener {
    @Bind(R.id.coordinator_main)
    CoordinatorLayout mCoordinatorLayout;
    @Bind(R.id.bottom_navigation)
    AHBottomNavigation mBottomBar;

    private long mPressedTime = 0;
    private FragmentManager mFragmentManager;
    private static final String TAG_HALL = "hall", TAG_LIST = "list", TAG_CENTER = "center";

    private static final int list_code = 11;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mFragmentManager = getSupportFragmentManager();
        mBottomBar.addItem(new AHBottomNavigationItem(getString(R.string.nav_home), DrawableUtils.getVectorDrawable(this, R.drawable.ic_home_main), Color.RED));
        mBottomBar.addItem(new AHBottomNavigationItem(getString(R.string.nav_list), DrawableUtils.getVectorDrawable(this, R.drawable.ic_list_main),Color.GREEN));
        mBottomBar.addItem(new AHBottomNavigationItem(getString(R.string.nav_user), DrawableUtils.getVectorDrawable(this, R.drawable.ic_user_main),Color.BLUE));
        mBottomBar.setAccentColor(ContextCompat.getColor(this, R.color.colorAccent));
        mBottomBar.setBehaviorTranslationEnabled(false);
        mBottomBar.setOnTabSelectedListener(this);
        mBottomBar.setCurrentItem(0);
        onTabSelected(0, false);
    }


    @Override
    public boolean onTabSelected(int position, boolean wasSelected) {
        if (wasSelected)
            return true;
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        final Fragment current = mFragmentManager.findFragmentByTag((String) mBottomBar.getTag());
        String tag = null;
        switch (position) {
            case 0:
                setTitle(R.string.nav_home);
                Fragment hallFragment = mFragmentManager.findFragmentByTag(tag = TAG_HALL);
                if (hallFragment == null) {
                    hallFragment = new HallFragment();
                    transaction.add(R.id.container_main, hallFragment, TAG_HALL);
                } else {
                    transaction.show(hallFragment);
                }
                break;
            case 1:
                if (!PalmSession.isLogin()) {
                    PalmSession.login(this, list_code);
                    transaction.commit();
                    return false;
                }
                setTitle(R.string.nav_list);
                Fragment listFragment = mFragmentManager.findFragmentByTag(tag = TAG_LIST);
                if (listFragment == null) {
                    listFragment = new OrderListFragment();
                    transaction.add(R.id.container_main, listFragment, TAG_LIST);
                } else {
                    transaction.show(listFragment);
                }
                break;
            case 2:
                setTitle(R.string.nav_user);
                Fragment centerFragment = mFragmentManager.findFragmentByTag(tag = TAG_CENTER);
                if (centerFragment == null) {
                    centerFragment = new CenterFragment();
                    transaction.add(R.id.container_main, centerFragment, TAG_CENTER);
                } else {
                    transaction.show(centerFragment);
                }
                break;
            default:
                break;
        }
        if (current != null) transaction.hide(current);
        transaction.commit();
        mBottomBar.setTag(tag);
        if (toolbar != null) {
            MenuItem item = toolbar.getMenu().findItem(R.id.action_settings);
            if (item != null)
                item.setVisible(position == mBottomBar.getItemsCount() - 1);
        }
        return true;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PalmSession.login_code) {
            if (RESULT_OK == resultCode) {
                Toast.makeText(this, R.string.prompt_login_succeed, Toast.LENGTH_SHORT).show();
            } else {
                if (mBottomBar.getCurrentItem() == 1)
                    mBottomBar.setCurrentItem(0);
            }
        } else if (requestCode == list_code) {
            if (RESULT_OK == resultCode) {
                mBottomBar.setCurrentItem(1);
            }
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (R.id.action_settings==item.getItemId())
            startActivity(new Intent(this,SettingsActivity.class));
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onBackPressed() {
        long time = System.currentTimeMillis();
        if (time - mPressedTime > 2000) {
            mPressedTime = time;
            Snackbar.make(mCoordinatorLayout, "再按一次退出", Snackbar.LENGTH_SHORT).show();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PalmSession.logout();
    }
}
